package som.simon.reactor;

import reactor.core.publisher.Flux;

import java.io.IOException;
import java.time.Duration;

public class ConcatMapMain {

    public static void main(String[] args) throws IOException {

        // Flux.just(1, 2, 3)
        //         .concatMap(ix -> Flux.just(String.format("A-%03d",ix), String.format("B-%03d",ix)))
        //         .log()
        //         .subscribe();

        // 拼接流
        Flux<Integer> integerFlux = Flux.range(1, 5).delayElements(Duration.ofSeconds(1));
        Flux<String> stringFlux = Flux.just("A", "B").delayElements(Duration.ofSeconds(2));

        // Flux.concat(integerFlux,stringFlux).subscribe(v -> System.out.println("v = " + v));

        /**
         * v = 1
         * v = 2
         * v = 3
         * v = 4
         * v = 5
         * v = A
         * v = B
         */

        // 合并流
        // Flux.merge(integerFlux,stringFlux).subscribe(v -> System.out.println("v = " + v));

        /**
         * v = 1
         * v = A
         * v = 2
         * v = 3
         * v = B
         * v = 4
         * v = 5
         */

        // 先发流后发流顺序
        Flux.mergeSequential(integerFlux,stringFlux).subscribe(v -> System.out.println("v = " + v));

        /**
         * v = 1
         * v = 2
         * v = 3
         * v = 4
         * v = 5
         * v = A
         * v = B
         */

        System.in.read();
    }

}
